package com.ex.touristtravels;

import com.ex.touristtravels.database.DatabaseHelper;
import com.ex.touristtravels.util.MyDialog;
import com.ex.touristtravels.util.MyDialog.onClickDialogListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity implements OnClickListener {

	private Button bt_ok; // 登录按钮
	private EditText et_user; // 账号编辑框
	private EditText et_password; // 密码编辑框
	private TextView tv;
	// private CheckBox autoLogin;// 自动登录 复选框
	// private CheckBox saveInfoItem;// 记住密码复选框

	private DatabaseHelper dbHelper; // 数据库操作对象
	private SharedPreferences sp; // 保存用户名、密码
	private static final int VERSION = 1; // 数据库历史版本
	private static final String DB_NAME = "UserInfo.db"; // 数据库名称

	private String uernameStr = null;
	private String passwordStr = null;

	private MyDialog dialogg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		dbHelper = new DatabaseHelper(this, DB_NAME, null, VERSION); // 实例化数据库操作对象
		sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE); // 初始化数据
		initView(); // 控件初始化
		// 初始化数据
		// LoadUserdata();

	}

	// 初始化数据
	private void LoadUserdata() {
		boolean checkstatus = sp.getBoolean("checkstatus", false);
		boolean auto = sp.getBoolean("auto", false);
		if (checkstatus) { // 判断是否曾经保存过密码
			String realUsername = sp.getString("username", "");
			String realPassword = sp.getString("password", "");
			if (!realUsername.equals("") && !(realUsername == null)
					|| (!realPassword.equals("")) || !(realPassword == null)) {

				et_user.setText("");
				et_password.setText("");
				et_user.setText(realUsername);
				et_password.setText(realPassword);
				// 添加复选框“记住密码” saveInfoItem.setChecked(true);
			}
			// else {
			// saveInfoItem.setChecked(true);
			// et_user.setText("");
			// et_password.setText("");
			// }
		}
		if (auto && checkstatus) {// 如果记住密码跟自动登录都被选中就选择登录跳转
			Intent intent = new Intent();
			intent.setClass(LoginActivity.this, StartActivity.class);
			startActivity(intent);
			finish();
		}
	}

	// 控件初始化
	private void initView() {
		// TODO Auto-generated method stub
		bt_ok = (Button) findViewById(R.id.btnOK);
		et_user = (EditText) findViewById(R.id.etUser);
		et_password = (EditText) findViewById(R.id.etPwd);
		tv = (TextView) findViewById(R.id.btnExperience);
		bt_ok.setOnClickListener(this);
		tv.setClickable(true);
		tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		uernameStr = et_user.getText().toString().trim();
		passwordStr = et_password.getText().toString().trim();
		if (login(uernameStr, passwordStr)) {
			// Editor editor = sp.edit();
			// if (saveInfoItem.isChecked()) {
			// // 保存用户名和密码
			// editor.putString("username", mUser.getText().toString().trim());
			// editor.putString("password",
			// mPassword.getText().toString().trim());
			// editor.putBoolean("checkstatus", true);
			// }
			// if (autoLogin.isChecked()) {
			// editor.putBoolean("auto", true);
			// }
			// editor.commit();

			Intent intent = new Intent();
			intent.setClass(LoginActivity.this, StartActivity.class);
			startActivity(intent);
			finish();
		} 
		else if (TextUtils.isEmpty(uernameStr) // 判断是否为空的
				|| TextUtils.isEmpty(passwordStr)) {
			on_click2();
		}
		else {
			on_click1();
		}
	}

	// 登录验证，将输入的用户和密码在数据中查找，肉能查找到至少一行的数据，返回到true.
	public boolean login(String user, String pass) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from User where username=? and password=?";
		Cursor cursor = db.rawQuery(sql, new String[] { user, pass });
		if (cursor.moveToFirst()) {
			cursor.close();
			return true;
		}
		return false;
	}

	//重新登录信息提示（登录失败）
	public void on_click1() {
		dialogg = new MyDialog(LoginActivity.this);
		dialogg.initUI("登录失败", "账号或者密码不正确", "重新填写");
		dialogg.setOnDialogClickListener(new onClickDialogListener() {

			@Override
			public void onclickButton() {
				dialogg.closeDialog();
			}
		});
		dialogg.showDialog();

	}
	//重新登录信息提示（登录错误）
	public void on_click2(){
		dialogg = new MyDialog(LoginActivity.this);
		dialogg.initUI("登录错误", "账号或者密码不能为空", "重新填写");
		dialogg.setOnDialogClickListener(new onClickDialogListener() {
			
			@Override
			public void onclickButton() {
				// TODO Auto-generated method stub
				dialogg.closeDialog();
			}
		});
		dialogg.showDialog();
	}
}
